home *** CD-ROM | disk | FTP | other *** search
- /*
- HandleUtils.c
-
- Created 28 Mar 1992 NeedHandle only
-
- Modified 14 Apr 1992 Added more functions
-
- */
-
- #include "HandleUtils.h"
-
- void ResizeHandle (Handle h, long newSize)
- {
- char hState;
-
- hState = HGetState (h);
- HUnlock (h);
- SetHandleSize (h, newSize);
- HSetState (h, hState);
- }
-
- char *GrowByAndPoint (Handle h, char *p, long growBy, long *length)
- {
- /* Given a handle to a block and a pointer into that block, grow the block by a
- specified amount and return 1) a pointer to the byte at the same offset from
- the beginning of the block as was the input pointer; and 2) the length of the
- grown block
- */
-
- long ofs = StripAddress (p) - StripAddress (*h);
- long oldLen = GetHandleSize (h), newLen;
-
- *length = newLen = oldLen + growBy;
- ResizeHandle (h, newLen);
- return *h + ofs;
- }
-
- Handle NeedHandle (Handle h, long size)
- {
- if (h == NULL)
- h = NewHandle (size);
- else if (*h == NULL)
- ReallocHandle (h, size);
- return h;
- }
-
- Handle AnyHandle (long size)
- {
- Handle h = NewHandle (size);
- OSErr err;
-
- return h ? h : TempNewHandle (size, &err);
- }
-
- OSErr HandleToScrap (Handle h, ResType type)
- {
- long err;
- char hState;
-
- err = ZeroScrap ();
- if (err == 0L) {
- hState = SmartHLock (h);
- err = PutScrap (GetHandleSize (h), 'TEXT', *h);
- HSetState (h, hState);
- }
- return (short) err;
- }
-
- char SmartHLock (Handle h)
- {
- char hState = HGetState (h);
-
- HLock (h);
- return hState;
- }